/**
 * @Author: ls <ls-mac>
 * @Date:   2019-08-13 17:59:48
 * @Last modified by:   ls-mac
 * @Last modified time: 2020-03-12 22:31:31
 */
import {
  packagePage,
  packageList,
  packageAddOrEdit,
  packageDetail,
  orderPackageList,
} from 'services/package';

export default {
  namespace: 'packageList',
  state: {
    data: {
      list: [],
      pagination: {},
    },
    detailData: {},
    packageList: [],
    orderPackageList: [],
  },

  effects: {
    *fetch({ payload, callback }, { call, put }) {
      const response = yield call(packagePage, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
    },
    *packageList({ payload, callback }, { call, put }) {
      const response = yield call(packageList, payload);
      yield put({
        type: 'savePackageList',
        payload: response,
      });
    },

    *packageAddOrEdit({ payload, callback }, { call, put }) {
      const response = yield call(packageAddOrEdit, payload);
      callback(response);
    },

    *deletePackageDetail({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveDeletePackageDetail',
      });
      if (callback) callback();
    },

    *packageDetail({ payload, callback }, { call, put }) {
      const response = yield call(packageDetail, payload);
      yield put({
        type: 'savePackageDetail',
        payload: response,
      });
    },

    *orderPackageList({ payload, callback }, { call, put }) {
      const response = yield call(orderPackageList, payload);
      yield put({
        type: 'saveOrderPackageList',
        payload: response,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    saveList(state, action) {
      const {
        data: { packageList, pageIndex, pageSize, recordCount },
      } = action.payload;

      const newList = packageList.map(v => {
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        data: {
          list: newList,
          pagination: {
            current: pageIndex,
            total: recordCount,
            pageSize,
          },
        },
      };
    },

    savePackageList(state, action) {
      const { data } = action.payload;
      const newList = data.map(val => {
        const v = val;
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        packageList: newList,
        orderPackageList: newList,
      };
    },
    saveOrderPackageList(state, action) {
      const { data } = action.payload;
      if (!data) {
        return {
          ...state,
        };
      }
      const newList = data.map(val => {
        const v = val;
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        orderPackageList: newList,
      };
    },

    saveDeletePackageDetail(state) {
      return {
        ...state,
        detailData: {},
      };
    },

    savePackageDetail(state, action) {
      const { data } = action.payload;

      if (data.storeFeatures) {
        data.storeFeatures = data.storeFeatures.split(',').map(v => {
          return v;
        });
      }
      if (data.extraItemList) {
        data.extraItemList = data.extraItemList.map(v => {
          return v.id;
        });
      }
      if (data.serviceList) {
        data.serviceList = data.serviceList.map(v => {
          return v.id;
        });
      }

      return {
        ...state,
        detailData: data,
      };
    },
  },
};
